







//
//  RecommendTableCell.swift
//  ledian
//
//  Created by Zhongnan on 16/2/29.
//  Copyright © 2016年 51ledian. All rights reserved.
//

import UIKit
/// 推荐牛人
class RecommendJobFinderCell: LDBaseRecFinderCell {
    
    let margin_V = LDMargin_V //竖直间隔
    let headImageWidth:CGFloat = 55 //kuan
    let BtnHeight:CGFloat = 20
    
    //上部
    var usertitleLab = UILabel.init()
    /** Button */
    var salaryLab = UILabel.init()
    var cityBtn = UIButton.init()
    var experienceBtn = UIButton.init()
    var eduBtn = UIButton.init()
    
    var divideLineView1 = UIImageView.setupDivideLine_H(LDFrame.ScreenWidth - Margin_V*10)
    
    //中部
    var headImageView = UIImageView.init()
    var jobfinderNameLab = UILabel.init()
    var jobfinderStatus = UILabel.init()
    
    var divideLineView2 = UIImageView.setupDivideLine_H(LDFrame.ScreenWidth - Margin_V*10)
    //下部
    var jobfinderDescLab:UILabel!
    var jobfinderVoiceStatus:UIButton!
    var locationBtn = UIButton()
    
    //jobmodels对象初始化
    internal var models = JobFinderInfoModel() {
        didSet{
            
            setContentSub()
            updateFrames()
        }
    }
    //frame
    private let btnMargin:CGFloat = 10
    let bottomLine = UIView()
    
    //MARK: - OVERRIDE
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.whiteColor()
        //布局
        setDefaultContent()
        setupFrame()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setContentSub(){
        
        let jobFinderInfo:JobFinderInfoModel = self.models
        
        //上
        usertitleLab.text = jobFinderInfo.expectjobtitle
        salaryLab.text = "¥\(jobFinderInfo.expectsalary)"
        
        cityBtn.setTitle(jobFinderInfo.expectcity, forState: .Normal)
        experienceBtn.setTitle(jobFinderInfo.experience, forState: .Normal)
        
        if jobFinderInfo.edu.characters.count < 1 {
            jobFinderInfo.edu = "学历"
        }
        eduBtn.setTitle(jobFinderInfo.edu, forState: .Normal)
        
        //中
        let api = LDCommentAPI.getPicApi(jobFinderInfo.logo)
        headImageView.sd_setImageWithURL(NSURL.init(string: api), placeholderImage: UIImage(named: "default_header.png"))
        let employername = jobFinderInfo.name.characters.count > 0 ? jobFinderInfo.name:"姓名"
        let sex = jobFinderInfo.sex.characters.count > 0 ? jobFinderInfo.sex:"性别"
        
        let nameLabStr = "\(employername) | \(sex) "
        let attr = NSMutableAttributedString(string: nameLabStr)
        let range = NSRange(location: 0, length: employername.characters.count)
        attr.addAttributes([NSForegroundColorAttributeName:LDFontColor.Green], range: range)
        jobfinderNameLab.attributedText = attr
        
        jobfinderStatus.text = jobFinderInfo.status.characters.count>0 ? jobFinderInfo.status:"工作状态"
        //下
        jobfinderDescLab.text = jobFinderInfo.mystrengthstext
        
        let isAudio = jobFinderInfo.isresumeaudio
        
        if isAudio == 1 {
            
            jobfinderVoiceStatus.selected = true
        } else {
            
            jobfinderVoiceStatus.selected = false
        }
        
    }
    //数据添加是更新
    func updateFrames() {
        
        dispatch_async(dispatch_get_global_queue(0, 0)) { [weak self] in
            
            if self == nil {
                return
            }
            
            let textsize = CGSizeMake(120, 13)
            
            let width1 = self!.models.expectcity.boundingRectWithSize(textsize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font12], context: nil).size.width
            let width2 = self!.models.experience.boundingRectWithSize(textsize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font12], context: nil).size.width
            let width3 = self!.models.edu.boundingRectWithSize(textsize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font12], context: nil).size.width
            
            dispatch_async(dispatch_get_main_queue(), {
                
                
                self!.cityBtn.snp_updateConstraints { (make) in
                    
                    make.width.equalTo(width1 + 30)
                }
                
                self!.experienceBtn.snp_updateConstraints { (make) in
                    make.width.equalTo(width2 + 35)
                    
                }
                
                self!.eduBtn.snp_updateConstraints { (make) in
                    make.width.equalTo(width3 + 35)
                }
                
                self!.setNeedsUpdateConstraints()
            })
        }
        
    }
    
    
    
    
    // MARK: - init 布局
    //添加view到主视图
    func setDefaultContent(){
        
        
        usertitleLab = UILabel.setHeadTitleLab("Title")
        
        //分割线1
        
        salaryLab = UILabel.setDetailsLab("", fontSize: 13, titleColor: LDFontColor.Red,Alignment: nil)
        
        cityBtn = UIButton.setLoactionBtn()
        experienceBtn = UIButton.setExperienceBtn()
        eduBtn = UIButton.setEduBtn()
        locationBtn = UIButton.setDistanceBtn()
        
        headImageView = UIImageView.setDefaultGoundImg("default_header_master.png", radius: headImageWidth/2)
        
        jobfinderNameLab.textColor = UIColor.grayColor()
        jobfinderNameLab.font = LDFont.Font13
        
        jobfinderStatus.textColor = UIColor.grayColor()
        jobfinderStatus.font = LDFont.Font13
        
        jobfinderDescLab = UILabel.setDetailsLab("描述", fontSize: 12, titleColor: LDFontColor.LightGray, Alignment: .Left)
        jobfinderDescLab.textColor = UIColor.grayColor()
        jobfinderDescLab.font = LDFont.Font12
        
        //
        jobfinderVoiceStatus = {
            
            let btn = UIButton()
            btn.setTitleColor(LDFontColor.Black, forState: .Normal)
            btn.setTitleColor(LDFontColor.Black, forState: .Normal)
            btn.setTitle("  文字简历", forState: .Normal)
            btn.setTitle("  语音简历", forState: .Selected)
            btn.titleLabel?.font = LDFont.Font14
            
            btn.setImage(UIImage.init(named: "common_text"), forState: .Normal)
            btn.setImage(UIImage.init(named: "common_voice"), forState: .Selected)
            
            return btn
        }()//UIButton.setDetailsBtn("  语音微简历", picName: "common_trumpet", btnBackColor: LDGolbalColor.White, titleColor: LDFontColor.LightGray)!
        jobfinderVoiceStatus.titleLabel?.font = LDFont.Font12
        
        locationBtn.titleLabel?.font = LDFont.Font12
        locationBtn.hidden = true
        
        contentView.addSubview(usertitleLab)
        contentView.addSubview(divideLineView1)
        contentView.addSubview(salaryLab)
        contentView.addSubview(cityBtn)
        contentView.addSubview(experienceBtn)
        contentView.addSubview(eduBtn)
        contentView.addSubview(headImageView)
        contentView.addSubview(jobfinderNameLab)
        contentView.addSubview(jobfinderStatus)
//        contentView.addSubview(divideLineView2)
//        contentView.addSubview(jobfinderDescLab)
        contentView.addSubview(jobfinderVoiceStatus)
//        contentView.addSubview(locationBtn)
    }
    
    //MARK - 自动布局
    func setupFrame(){
        
        
        //头部
        headImageView.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(margin_V*3)
            make.left.equalTo(margin_V*5)
            make.height.width.equalTo(headImageWidth)
        })
        
        jobfinderNameLab.snp_remakeConstraints(closure: { (make) in
            
            make.centerY.equalTo(headImageView)
            make.left.equalTo(headImageView.snp_right).offset(LDMargin_H*2)
            make.height.equalTo(15)
        })
        
        divideLineView1.snp_remakeConstraints(closure: { (make) in
            
            make.top.equalTo(headImageView.snp_bottom).offset(LDMargin_H)
            make.height.equalTo(0.5)
            make.left.equalTo(LDMargin_V*2)
            make.right.equalTo(-LDMargin_V*2)
        })
        
        //----------------------------------
        usertitleLab.snp_makeConstraints(closure: { (make) in
            
            
            make.top.equalTo(divideLineView1.snp_bottom).offset(LDMargin_V*2)
            make.left.equalTo(headImageView)
            make.height.equalTo(20)
            
        })
        
        salaryLab.snp_makeConstraints(closure: { (make) in
            
            make.centerY.equalTo(usertitleLab)
            make.right.equalTo(-LDMargin_V*3)
            make.height.equalTo(20)
        })
        
        //
        jobfinderVoiceStatus.snp_makeConstraints { (make) in
            
            make.left.equalTo(usertitleLab).offset(-LDMargin_H-5)
            make.top.equalTo(usertitleLab.snp_bottom).offset(LDMargin_V*2)
            make.size.equalTo(CGSizeMake(90, 28))
        }
        
        jobfinderStatus.snp_makeConstraints { (make) in
            
            make.left.equalTo(jobfinderVoiceStatus.snp_right).offset(LDMargin_V*2)
            make.centerY.height.equalTo(jobfinderVoiceStatus)
        }
        
        //下部-------------------
        cityBtn.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(jobfinderVoiceStatus.snp_bottom).offset(LDMargin_V*1.5)
            make.left.equalTo(usertitleLab).offset(-3)
            make.height.equalTo(15)
        })
        
        experienceBtn.snp_makeConstraints { (make) in
            
            make.top.centerY.height.equalTo(cityBtn)
            make.left.equalTo(cityBtn.snp_right).offset(LDMargin_V)
        }
        
        eduBtn.snp_makeConstraints { (make) in
            
            make.top.centerY.height.equalTo(experienceBtn)
            make.left.equalTo(experienceBtn.snp_right).offset(LDMargin_V)
        }
        
        
//        //头部
//        usertitleLab.snp_makeConstraints(closure: { (make) in
//            
//            make.top.equalTo(margin_V*3)
//            make.left.equalTo(margin_V*5)
//            make.height.equalTo(20)
//        })
//        
//        salaryLab.snp_makeConstraints(closure: { (make) in
//            
//            make.top.equalTo(usertitleLab.snp_bottom).offset(10)
//            make.left.equalTo(usertitleLab)
//            make.height.equalTo(12)
//        })
//        
//        cityBtn.snp_makeConstraints(closure: { (make) in
//            
//            make.height.equalTo(13)
//            make.centerY.height.equalTo(salaryLab)
//            make.left.equalTo(salaryLab.snp_right).offset(margin_V)
//        })
//        
//        experienceBtn.snp_makeConstraints(closure: { (make) in
//            
//            make.height.equalTo(12)
//            make.top.equalTo(cityBtn)
//            make.left.equalTo(cityBtn.snp_right).offset(margin_V)
//            
//        })
//        
//        eduBtn.snp_makeConstraints(closure: { (make) in
//            
//            make.height.equalTo(12)
//            make.top.equalTo(cityBtn)
//            make.left.equalTo(experienceBtn.snp_right).offset(margin_V)
//        })
//        //分割线
//        divideLineView1.snp_makeConstraints { (make) in
//            
//            make.top.equalTo(salaryLab.snp_bottom).offset(margin_V*1.5)
//            make.height.equalTo(1)
//            make.left.equalTo(LDMargin_H*4)
//            make.right.equalTo(-LDMargin_H*4)
//        }
//        
//        //中间
//        headImageView.snp_makeConstraints(closure: { (make) in
//            
//            make.top.equalTo(divideLineView1.snp_bottom).offset(margin_V*1.5)
//            make.left.equalTo(usertitleLab)
//            make.width.height.equalTo(headImageWidth)
//        })
//        jobfinderNameLab.snp_makeConstraints(closure: { (make) in
//            
//            make.top.equalTo(headImageView).offset(margin_V*1.5)
//            make.left.equalTo(headImageView.snp_right).offset(10)
//            make.height.equalTo(20)
//        })
//        jobfinderStatus.snp_makeConstraints(closure: { (make) in
//            
//            make.top.equalTo(jobfinderNameLab.snp_bottom).offset(margin_V*0.6)
//            make.left.equalTo(jobfinderNameLab)
//            make.width.equalTo(200)
//            make.height.equalTo(15)
//        })
//        
//        //分割线
//        divideLineView2.snp_makeConstraints { (make) in
//            
//            make.top.equalTo(headImageView.snp_bottom).offset(margin_V*1.8)
//            make.height.equalTo(1)
//            make.left.equalTo(LDMargin_H*4)
//            make.right.equalTo(-LDMargin_H*4)
//        }
//        //下部
//        jobfinderDescLab.snp_makeConstraints(closure: { (make) in
//            
//            make.top.equalTo(divideLineView2.snp_bottom).offset(10)
//            make.left.equalTo(headImageView).offset(-5)
//            make.width.equalTo(LDFrame.ScreenWidth - 40)
//            make.height.equalTo(15)
//        })
//        
//        jobfinderVoiceStatus.snp_makeConstraints { (make) in
//            
//            make.top.equalTo(divideLineView2.snp_bottom).offset(10)
//            make.left.equalTo(headImageView).offset(-5)
//            make.width.equalTo(90)
//            make.height.equalTo(15)
//        }
//        
//        locationBtn.snp_makeConstraints(closure: { (make) in
//            
//            make.top.equalTo(jobfinderDescLab)
//            make.right.equalTo(-20)
//            make.width.equalTo(40)
//            make.height.equalTo(15)
//        })
    }
}